package com.myexam.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myexam.dao.SubjectDao;
import com.myexam.dao.TeacherDao;
import com.myexam.model.Exam;
import com.myexam.model.Score;
import com.myexam.model.Student;
import com.myexam.model.StudentResult;
import com.myexam.model.Subject;
import com.myexam.model.Teacher;
import com.myexam.service.StudentService;
import com.myexam.service.TeacherService;
import com.myexam.util.Page;
import com.myexam.util.PageResult;
import com.myexam.util.PageUtil;
@Service
public class TeahcerServiceimpl implements TeacherService {
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private StudentService studentService;
	@Override
	public boolean allowLogin(String teacherID, String password) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findByTeacherID(teacherID);
		if(teacher == null){
			return false;
		}else{
			if(!password.equals(teacher.getPassword())){
				return false;
			}
		}
		return true;
	}
	public Teacher getTeacherInfo(String id){
		return teacherDao.findByTeacherID(id);
	}
	
	@Override
	public boolean saveExam(Exam exam,Teacher teacher) {
		// TODO Auto-generated method stub
		exam.setExam_author(teacher.getTeacherName());
		teacherDao.addExam(exam);
		return true;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResult queryExamByPage(Page page){
		page = PageUtil.createPage(page.getEveryPage(), teacherDao.findExamCount(), page.getCurrentPage());
		List<Exam> list = teacherDao.findExamByPage(page);
		PageResult pageResult = new PageResult(page,list);
		return pageResult;
	}
	
	@Override
	public List<Subject> queryExamSubject(int examID){
		// TODO Auto-generated method stub
		
		Exam exam = teacherDao.findExamByID(examID);
		String[] strs = exam.getExam_subject().split(",");
		List<Subject> subjects = new ArrayList<>();
		for(int i =1;i<strs.length;i++){
			subjects.add(subjectDao.findSubjectByID(Integer.parseInt(strs[i])));
		}
		return subjects;
	}
	
	public Exam queryExam(int examID){
		return teacherDao.findExamByID(examID);
	}
	@Override
	public List<Exam> findExam() {
		// TODO Auto-generated method stub
		return teacherDao.findExam();
	}
	@Override
	public void deleteExam(int examID) {
		// TODO Auto-generated method stub
		teacherDao.deleteExam(examID);
	}
	
	@Override
	public Exam examUpdateShow(int examID) {
		// TODO Auto-generated method stub
		return teacherDao.examUpdateShow(examID);
	}
	@Override
	public void examUpdate(int examID,int examStage) {
		// TODO Auto-generated method stu
		Exam exam = teacherDao.examUpdateShow(examID);
		exam.setExam_stage(examStage);
		teacherDao.updateExam(exam);
	}
	
	@Override
	public boolean exam(int studentID,int examID) {
		// TODO Auto-generated method stub
		List<Score> scores = teacherDao.findScoreByID(studentID);
		if(scores == null){
			return true;
		}else {
			for (int i = 0; i < scores.size(); i++) {
				if(scores.get(i).getExamID() == examID ){
					return false;
				}
			}
		}
		return true;
	}
	
	public List<StudentResult> studentResults(int studentID){
		List<Score> scores = teacherDao.findScoreByID(studentID);
		Student student = studentService.getStudentInfo(String.valueOf(studentID));
		List<StudentResult> studentResults = new ArrayList<>();
		
		for(int i = 0;i<scores.size();i++){
			StudentResult studentResult = new StudentResult();
			Exam exam = teacherDao.findExamByID(scores.get(i).getExamID());
			studentResult.setExamName(exam.getExam_name());
			studentResult.setStudentName(student.getStudentName());
			studentResult.setTime(scores.get(i).getTime());
			studentResult.setResult(exam.getExam_score());
			studentResult.setScore(String.valueOf(scores.get(i).getScore()));
			studentResults.add(studentResult);
		}
		return studentResults;
	}
}
